package com.jy.event.timer;

import com.jy.event.dao.WorkShiftDao;
import com.jy.event.domain.WorkShift;
import com.jy.event.util.GateweyUtils;
import com.jy.event.util.LocalDateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by lihu on 2018/3/24.
 */

@Slf4j
@Component
public class WorkShiftTimer {

    @Autowired
    WorkShiftDao workShiftDao;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    GateweyUtils gateweyUtils;


    /**
     * 轮询交班时长
     */
    @Scheduled(cron = "0 0 */3 * * ?")  //每1小时执行一次
    public void workShift() {
        log.info("workShift start");
        LocalDateTime dateTime = LocalDateTime.now();
        List<WorkShift> workShifts = workShiftDao.findByClosedAndBeginTimeBefore(false,
                LocalDateTimeUtils.convertLDTToDate(dateTime.minusHours(48)));
        for (WorkShift workShift : workShifts) {

            try {
                restTemplate.postForEntity(gateweyUtils.workShift().
                        replace("ID", workShift.getId()), null, Object.class);
            } catch (Exception ex) {
                log.error("workShift is error:{}", workShift);
            }

        }

    }


}
